<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="plotId" label="小区名称：">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区名称" clearable style="width: 170px"
          @change="xiaoquChange()">
          <el-option v-for="dict in xiaoquData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="buildId" label="所属楼栋：">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋" clearable style="width: 170px"
          @change="loudonChange()">
          <el-option v-for="dict in loudongData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="unitId" label="所属单元：">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable style="width: 170px">
          <el-option v-for="dict in danyuanData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期：" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div :gutter="10" class="mb8" style="margin-bottom: 10px;display: flex;justify-content: space-between;">
      <div>
        <el-button type="primary" plain size="mini" icon="el-icon-refresh" @click="piliangHuiFu">批量恢复</el-button>
        <el-button type="primary" plain size="mini" icon="el-icon-circle-close" @click="piliangDel">批量删除</el-button>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <el-table v-loading="loading" :data="archivesList" @selection-change="handleSelectionChange">
      <el-table-column v-if="selectFlag" type="selection" width="55" align="center" />
      <el-table-column label="所属小区" align="center" prop="plotName" />
      <el-table-column label="存档类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.archives_type" :value="Number(scope.row.type)" />
        </template>
      </el-table-column>
      <el-table-column label="阀门开度" align="center" prop="valveOpen" />
      <el-table-column label="存档详情" align="center" prop="info" />
      <el-table-column label="存档备注" align="center" prop="remark" />
      <el-table-column label="存档人" align="center" prop="createName" />
      <el-table-column label="存档时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:archives:edit']">查看</el-button>
          <el-button size="mini" type="text" @click="huifu(scope.row)"
            v-hasPermi="['equipment:archives:edit']">恢复</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:archives:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改存档对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :append-to-body="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="存档类型1小区2楼栋3单元" prop="type">-->
        <!--<el-select v-model="form.type" placeholder="请选择存档类型1小区2楼栋3单元">-->
        <!--<el-option-->
        <!--v-for="dict in dict.type.archives_type"-->
        <!--:key="dict.value"-->
        <!--:label="dict.label"-->
        <!--:value="parseInt(dict.value)"-->
        <!--&gt;</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="小区1" prop="plotId">
          <el-input disabled v-model="form.plotName" placeholder="请输入小区" />
        </el-form-item>
        <el-form-item label="阀门开度" prop="valveOpen">
          <el-input disabled v-model="form.valveOpen" placeholder="请输入阀门开度" />
        </el-form-item>
        <el-form-item label="存档详情" prop="buildId">
          <el-input disabled v-model="form.info" placeholder="存档详情" />
        </el-form-item>
        <el-form-item label="存档时间" prop="unitId">
          <el-input disabled v-model="form.createTime" placeholder="存档时间" />
        </el-form-item>
        <el-form-item label="存档备注" prop="remark">
          <el-input disabled v-model="form.remark" type="textarea" placeholder="存档备注" />
        </el-form-item>
      </el-form>
      <!--<div slot="footer" class="dialog-footer">-->
      <!--<el-button type="primary" @click="submitForm">确 定</el-button>-->
      <!--<el-button @click="cancel">取 消</el-button>-->
      <!--</div>-->
    </el-dialog>
  </div>
</template>

<script>
import { listArchives, getArchives, delArchives, addArchives, updateArchives, restoreArchives } from "@/api/equipment/archives";
import { listCPlotSelect, listBuildSelect, listBuildUnitSelect, listCustomModelSelect } from "@/api/system/utils"
import { addEquipmentArchivesRestore, addRestore } from "@/api/equipment/equipmentArchivesRestore";
import { number } from 'echarts';


export default {
  name: "Archives",
  dicts: ['archives_type'],
  data() {
    return {
      xiaoquData: [],
      loudongData: [],
      danyuanData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存档表格数据
      archivesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 处理备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plotId: null,
        buildId: null,
        unitId: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "存档类型1小区2楼栋3单元不能为空", trigger: "change" }
        ],
        valveOpen: [
          { required: true, message: "阀门开度不能为空", trigger: "blur" }
        ],
        plotId: [
          { required: true, message: "小区id不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ]
      },
      ids: [],
      selectFlag: false,

    };
  },
  created() {
    console.log(this.dict)
    listCPlotSelect({}).then(res => {
      this.xiaoquData = res.rows
      this.queryParams.plotId = res.rows[0].value
    }).then(res=>{
      listBuildSelect({ plotId:this.queryParams.plotId}).then((res) => {
          this.loudongData = res.rows;
        });
      this.getList();
    });
  },
  methods: {
    xiaoquChange() {
      listBuildSelect({ plotId: this.queryParams.plotId }).then(res => {
        this.queryParams.buildId = null;
        this.queryParams.unitId = null;
        this.loudongData = res.rows
        this.danyuanData = []
      });
    },
    loudonChange() {
      listBuildUnitSelect({ buildId: this.queryParams.buildId }).then(res => {
        this.queryParams.unitId = null;
        this.danyuanData = res.rows
      });
    },
    // 批量删除
    piliangDel() {
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        const ids = this.ids.map(item => item.id);
        this.$modal.confirm('是否删除所选数据？').then(function () {
          return delArchives(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }
    },
    // 批量恢复
    piliangHuiFu() {
      let ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        ids = ids.map(item => {
          item.archivesId = item.id
          return item
        })
        this.$modal.confirm('确定要恢复选中的数据吗？恢复后将按照此更新数据。').then(function () {
          return restoreArchives(ids);
        }).then(() => {
          this.$modal.msgSuccess("批量恢复成功");
          this.selectFlag = false
          this.getList();
        }).catch(() => { });
      }
    },
    /** 查询存档列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listArchives(this.queryParams).then(response => {
        this.archivesList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },
    /** 恢复 */
    huifu(row) {
      row.archivesId = row.id
      let data = []
      data.push(row)
      restoreArchives(data).then(res => {
        this.$message.success("恢复成功");
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        valveOpen: null,
        plotId: null,
        buildId: null,
        unitId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.queryParams.createTime = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存档";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(121)
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "查看";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArchives(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArchives(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否删除当前数据？').then(function () {
        return delArchives(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipment/archives/exportEquipmentArchives', {
        ...this.queryParams
      }, `archives_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
